package com.chinasoftosg.sicap.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.chinasoftosg.sicap.pojo.AdminInfo;
import com.chinasoftosg.sicap.pojo.Rights;
import com.chinasoftosg.sicap.pojo.mapper.BillingMapper;
import com.chinasoftosg.sicap.services.ILoginService;
import com.chinasoftosg.sicap.utils.CommonUtil;
import com.chinasoftosg.sicap.utils.ConstantUtil;
import com.chinasoftosg.sicap.utils.bean.AdminInfoBean;
import com.chinasoftosg.sicap.utils.taglib.PageResult;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * 登录页面
 * 登录页面
 * @author ZhangBo
 * @version 2012-4-27
 * @see LoginAction
 * @since
 */
public class LoginAction extends ActionSupport
{
    /**
     * 意义，目的和功能，以及被用到的地方<br>
     */
    private static final long serialVersionUID = -3037030893499991147L;
    
    private ILoginService loginService;
    
    public void setLoginService(ILoginService loginService)
    {
        this.loginService = loginService;
    }
    
    private String loginName;
    
    private String loginPwd;
    
    private String msg;
    
    public String getLoginName()
    {
        return loginName;
    }
    
    public void setLoginName(String loginName)
    {
        this.loginName = loginName;
    }
    
    public String getLoginPwd()
    {
        return loginPwd;
    }
    
    public void setLoginPwd(String loginPwd)
    {
        this.loginPwd = loginPwd;
    }
    
    public String getMsg()
    {
        return msg;
    }
    
    public void setMsg(String msg)
    {
        this.msg = msg;
    }
    
    /**
     * 
     * Description:登录页面 <br>
     * 1、…<br>
     * 2、…<br>
     * Implement: <br>
     * 1、…<br>
     * 2、…<br>
     * 
     * @return
     * @see
     */
    public String login()
    {
        removeAdminInfoBean();
        
        //判断非空
        if (StringUtils.isEmpty(getLoginName())
                || StringUtils.isEmpty(getLoginPwd()))
        {
            this.setMsg("请输入用户名和密码！");
            return Action.INPUT;
        }
        //判断用户名和密码
        AdminInfo adminInfo = this.loginService.login(this.getLoginName(),
                this.getLoginPwd());
        if (CommonUtil.isEmpty(adminInfo))
        {
            this.setMsg("用户名或密码错误！");
            return Action.INPUT;
        }
        
        HttpSession session = ServletActionContext.getRequest().getSession();
        
        List<Rights> rights = null;
        if (adminInfo.getRoleId() == 1)
        {
            rights = loginService.getRightsListAll();
        }
        else
        {
            //验证通过、获取用户信息
            rights = loginService.getRightsList(adminInfo.getRoleId());
        }
        //获取权限信息
        AdminInfoBean adminInfoBean = new AdminInfoBean();
        
        adminInfoBean.setAdminInfo(adminInfo);
        adminInfoBean.setRights(rights);
        session.setAttribute(ConstantUtil.ADMININFO_BEAN, adminInfoBean);
        return Action.SUCCESS;
    }
    
    public String main()
    {
        return "main";
    }
    
    /**
     * 
     * Description: 用户登出<br>
     * 1、…<br>
     * 2、…<br>
     * Implement: <br>
     * 1、…<br>
     * 2、…<br>
     * 
     * @return 
     * @see
     */
    public String logout()
    {
        //清除用户会话
        removeAdminInfoBean();
        return Action.INPUT;
    }
    
    /**
     * 清除用户会话权限
     * Description: <br>
     * 1、…<br>
     * 2、…<br>
     * Implement: <br>
     * 1、…<br>
     * 2、…<br>
     *  
     * @see
     */
    private void removeAdminInfoBean()
    {
        ServletActionContext.getRequest()
                .getSession()
                .removeAttribute(ConstantUtil.ADMININFO_BEAN);
    }
    
    public String nav()
    {
        return "left_nav";
    }
    
    public String top()
    {
        return "top_info";
    }
    
    private PageResult pageResult = new PageResult();
    
    public PageResult getPageResult()
    {
        return pageResult;
    }
    
    public void setPageResult(PageResult pageResult)
    {
        this.pageResult = pageResult;
    }
    
    public String testPage()
    {
        this.pageResult.setSql("select * from T_BILLING");
        this.loginService.listPage(this.pageResult, new BillingMapper());
        return "test_page";
    }
}
